package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.utils.BeanHelper;
import org.apache.ibatis.annotations.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private SetmealMapper setmealMapper;

    /**
     * @description: 添加购物车
     * @author: tanbobin
     * @date: 2024/6/5 20:19
     * @param:
     * @param shoppingCartDTO
     * @return:
     **/
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //1.查询 当前用户的购物车中是否存在指定的商品(菜品-口味，套餐)
        Long currentId = BaseContext.getCurrentId();    // 获取当前用户id
        ShoppingCart shoppingCart = BeanHelper.copyProperties(shoppingCartDTO, ShoppingCart.class);
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        //2.如果该商品在当前用户的购物车已经存在，则数量+1
        if (!CollectionUtils.isEmpty(shoppingCartList)){
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //3.如果该商品在当前用户的购物车不存在，则添加一条新的购物车数据
            Long dishId = shoppingCart.getDishId();
            if (dishId != null){ // 菜品
                Dish dish = dishMapper.getDishInfo(dishId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
            } else {    //套餐
                Setmeal setmeal = setmealMapper.getSetmealById(shoppingCart.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);

            // 插入数据
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * @description: 查看购物车
     * @author: tanbobin
     * @date: 2024/6/5 21:28
     * @param:
     * @return:
     * @return java.util.List<com.sky.entity.ShoppingCart>
     **/
    @Override
    public List<ShoppingCart> list() {
        // 获取用户id
        Long userId = BaseContext.getCurrentId();

        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        List<ShoppingCart> cartList = shoppingCartMapper.list(shoppingCart);

        return cartList;
    }

    /**
     * @description: 删除购物车一个商品
     * @author: tanbobin
     * @date: 2024/6/5 22:03
     * @param:
     * @return:
     **/
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        //1. 查询当前用户、当前操作的菜品/套餐的购物车列表数据
        Long userId = BaseContext.getCurrentId(); // 获取用户id
        ShoppingCart shoppingCart = BeanHelper.copyProperties(shoppingCartDTO, ShoppingCart.class);
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //2. 判断当前购物车商品的数量 , 如果 > 1 , 就将数量减一, 然后更新数据库
        if (!CollectionUtils.isEmpty(list)){

            ShoppingCart cart = list.get(0);

            if (cart.getNumber() > 1) {
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(cart);
            } else {
                //3. 如果 = 1, 就删除该购物车商品
                shoppingCartMapper.deleteById(cart.getId());
            }
        }
    }

    /**
     * @description: 清空购物车
     * @author: tanbobin
     * @date: 2024/6/5 22:12
     * @param:
     * @return:
     **/
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        shoppingCartMapper.delete(shoppingCart);
    }
}









